package com.helin.helinhealth.common;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 全局响应处理器
 * 
 * <p>
 * 该类拦截所有控制器方法的返回结果，确保API响应格式统一。
 * 如果返回的不是Result类型，会自动将其包装为Result类型。
 * </p>
 */
@RestControllerAdvice(basePackages = {"com.helin.helinhealth.controller"})
public class ResponseAdvice implements ResponseBodyAdvice<Object> {

    /**
     * 判断是否需要处理响应体
     * 
     * @param returnType 返回类型
     * @param converterType 消息转换器类型
     * @return 是否支持处理
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        // 如果返回类型已经是Result或ResponseEntity，则不需要处理
        // 排除Swagger和Knife4j相关的类型
        String returnTypeName = returnType.getParameterType().getName();
        return !returnType.getParameterType().equals(Result.class)
                && !ResponseEntity.class.isAssignableFrom(returnType.getParameterType())
                && !returnTypeName.contains("springfox")
                && !returnTypeName.contains("springdoc")
                && !returnTypeName.contains("swagger")
                && !returnTypeName.contains("knife4j");
    }

    /**
     * 在写入响应之前修改响应体
     * 
     * @param body 原始响应体
     * @param returnType 返回类型
     * @param selectedContentType 选择的内容类型
     * @param selectedConverterType 选择的转换器类型
     * @param request 请求
     * @param response 响应
     * @return 修改后的响应体
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                 Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                 ServerHttpRequest request, ServerHttpResponse response) {
        // 对于某些特殊类型的响应，可以直接返回不做处理
        if (body == null) {
            return Result.success();
        }
        
        // 排除Swagger和Knife4j相关的路径
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String path = servletRequest.getRequestURI();
            if (path.contains("/v3/api-docs") 
                || path.contains("/swagger") 
                || path.contains("/doc.html")
                || path.contains("/webjars/")
                || path.contains("/favicon.ico")) {
                return body;
            }
        }
        
        // 获取请求路径
        String path = "";
        if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            path = servletRequest.getRequestURI();
        }
        
        // 将响应包装为Result类型
        return Result.success("操作成功", body);
    }
} 